//
//  MyViewController.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/14.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "MyViewController.h"
#import "LTFMyCollectViewController.h"
#import "IsNight.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MyViewController ()
<
 UITableViewDataSource,
 UITableViewDelegate
>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIView *headView;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    [self creatnight];
}
- (void)viewWillAppear:(BOOL)animated {
    if ([IsNight nowIsNight]) {
        self.navigationController.navigationBar.barTintColor = [IsNight nightColor];
        self.view.backgroundColor = [IsNight nightColor];
        self.tableView.backgroundColor = [IsNight nightColor];
        self.tabBarController.tabBar.barTintColor = [IsNight nightColor];
    }
    else {
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        self.view.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    }
}
- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    
    //self.tableView.rowHeight = 80;
    self.array = [NSMutableArray arrayWithObjects:@"我的收藏",@"清除缓存",@"夜间模式", @"分享", nil];

    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [self.view addSubview:_headView];
    [_headView release];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [_headView addSubview:_imageView];
    _imageView.image = [UIImage imageNamed:@"1.jpg"];
    [_imageView release];
    _tableView.tableHeaderView = _headView;
    
    
}
- (void)creatnight
{
    UISwitch *night = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH - 70, 44 * 2 + HEIGHT / 3 + 5, 100, 50)];
    night.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    
    
    [night addTarget:self action:@selector(nightAction:) forControlEvents:UIControlEventValueChanged];
    
    
    [self.tableView addSubview:night];
    
}
-(void)nightAction:(UISwitch *)sender {
    
    [IsNight changisNight];
    [self viewWillAppear:YES];
    [self.tableView reloadData];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if ([IsNight nowIsNight]) {
        cell.textLabel.textColor = [IsNight textNightColor];
        cell.backgroundColor = [IsNight nightColor];
        
    }
    else {
        cell.textLabel.textColor = [UIColor blackColor];
        cell.backgroundColor = [UIColor whiteColor];
    }
    cell.textLabel.text = self.array[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        LTFMyCollectViewController *collectVC = [[LTFMyCollectViewController alloc] init];
        [self.navigationController pushViewController:collectVC animated:YES];
        [collectVC release];
    }
    else if (indexPath.row == 1) {
            NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES) lastObject];
            NSFileManager *manager = [NSFileManager defaultManager];
            [manager removeItemAtPath:sandBox error:nil];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已清除" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [alert show];
        }
    else if (indexPath.row == 3) {

        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
